package cn.vshare.util;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;


@Controller
public class GenerateCodeUtil {
    private int width=110;//验证码宽度
    private int height=40;//验证码高度
    private int codeCount=1;//验证码个数,默认4个
    private int lineCount=19;//干扰线个数

    char[] codeSequence= { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    /**
     * 获取验证码
     * @param request
     * @param response
     * @throws IOException
     */
    //http://localhost:8080/getCode111.do

    @RequestMapping(value = "getCode111")
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //定义随机数类
        Random random=new Random();
        //定义存储验证码的类
        StringBuilder builderCode=new StringBuilder();
        //定义画布
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到画笔
        Graphics graphics=bufferedImage.getGraphics();
        //1-设置颜色,画边框
        graphics.setColor(Color.black);
        graphics.drawRect(0,0,width,height);
        //2-设置颜色,填充内部
        graphics.setColor(Color.WHITE);
        graphics.fillRect(1,1,width-2,height-2);
        //3-设置干扰线
        graphics.setColor(Color.GRAY);
        for (int i=0;i<lineCount;i++){
            //暂时不使用,便于测试
            //graphics.drawLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height));
        }
        //4-设置验证码(全局)
        graphics.setColor(Color.BLUE);
        //4.1-设置验证码的字体
        graphics.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
        for (int i=0;i<codeCount;i++){
            char c=codeSequence[random.nextInt(codeSequence.length)];
            builderCode.append(c);

            // 产生随机的颜色分量来构造颜色值，这样输出的每位数字的颜色值都将不同。
            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);

            // 用随机产生的颜色将验证码绘制到图像中。
            graphics.setColor(new Color(red, green, blue));
            graphics.drawString(c+"",20*(i+1),20);
        }
        //5-保存到Session中
        HttpSession session=request.getSession();
        session.setAttribute("codeValidate",builderCode.toString());

        //7-禁止图片缓存
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/png");

        //6-输出到屏幕
        ServletOutputStream servletOutputStream= response.getOutputStream();
        ImageIO.write(bufferedImage,"png",servletOutputStream);
        //8-关闭
        servletOutputStream.flush();

    }
}
